package cz.muni.fi.pb138.evidenceskladurestaurace;

import cz.muni.fi.pb138.evidenceskladurestaurace.model.IngredientsTableModel;
import cz.muni.fi.pb138.evidenceskladurestaurace.model.RecipeListModel;
import cz.muni.fi.pb138.evidenceskladurestaurace.persistence.Ingredient;
import cz.muni.fi.pb138.evidenceskladurestaurace.persistence.IngredientDAO;
import cz.muni.fi.pb138.evidenceskladurestaurace.persistence.IngredientDAOImpl;
import cz.muni.fi.pb138.evidenceskladurestaurace.persistence.Recipe;
import cz.muni.fi.pb138.evidenceskladurestaurace.persistence.RecipeDAO;
import cz.muni.fi.pb138.evidenceskladurestaurace.persistence.RecipeDAOImpl;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Adams
 */
public class ReceiptDialog extends javax.swing.JFrame {

    private RecipeDAO recipeDAO = new RecipeDAOImpl();
    private IngredientDAO ingredientDAO = new IngredientDAOImpl();
    private IngredientsTableModel ingredientsTableModel = new IngredientsTableModel();
    private IngredientsTableModel ingredientsTableModelRestaurant = new IngredientsTableModel();
    private RecipeListModel recipeListModel = new RecipeListModel();
    private List<Ingredient> recipeIngredientList = new ArrayList<>();
    private Recipe receipt = null;
    private Ingredient ingredient = null;
    private Boolean createNew = true;


    //CREATE
    public ReceiptDialog(RecipeListModel receiptListModel, RecipeDAO receiptDao, IngredientDAO ingredientDAO) {
        initComponents();
        this.recipeListModel = receiptListModel;
        this.recipeDAO = receiptDao;
        this.ingredientDAO = ingredientDAO;
        setLocationRelativeTo(null);
        receipt = new Recipe();

        ingredientsTableModel.setIngredients(recipeIngredientList);
        ingredienceTable.setModel(ingredientsTableModel);

        editIngredient.setVisible(false);
        deleteIngredient.setVisible(false);
    }

    //EDIT
    public ReceiptDialog(RecipeListModel receiptListModel,IngredientsTableModel ingredientsTableModelRestaurant, RecipeDAO receiptDao, IngredientDAO ingredientDAO, Recipe receipt) {
        initComponents();
        this.receipt = receipt;
        this.recipeListModel = receiptListModel;
        this.recipeDAO = receiptDao;
        this.ingredientDAO = ingredientDAO;
        this.ingredientsTableModelRestaurant = ingredientsTableModelRestaurant;
        setLocationRelativeTo(null);

        recipeIngredientList = receipt.getIngredients();
        ingredientsTableModel.setIngredients(recipeIngredientList);
        ingredienceTable.setModel(ingredientsTableModel);

        createNew = false;

        receiptLabel.setText("Edit Receipt");
        receiptName.setText(receipt.getName());
        receiptName.setEditable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        receiptName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        addIngredientButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        receiptLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ingredienceTable = new javax.swing.JTable();
        editIngredient = new javax.swing.JButton();
        deleteIngredient = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Receipt Name:");

        jLabel2.setText("Ingredients :");

        addIngredientButton.setText("Add Ingedient");
        addIngredientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addIngredientButtonActionPerformed(evt);
            }
        });

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        receiptLabel.setText("Create new Receipt");

        ingredienceTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(ingredienceTable);

        editIngredient.setText("Edit Ingredient");
        editIngredient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editIngredientActionPerformed(evt);
            }
        });

        deleteIngredient.setText("Delete Ingredient");
        deleteIngredient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteIngredientActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(234, 234, 234)
                .add(receiptLabel)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(37, 37, 37)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 526, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(26, 26, 26)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(addIngredientButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(editIngredient, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(deleteIngredient, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(0, 89, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2)
                            .add(saveButton))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(42, 42, 42)
                                .add(receiptName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 288, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(15, 15, 15)
                                .add(cancelButton)))
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(15, 15, 15)
                .add(receiptLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(receiptName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel2)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(12, 12, 12)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 396, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(36, 36, 36)
                        .add(addIngredientButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(editIngredient)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(deleteIngredient)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 45, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(saveButton)
                    .add(cancelButton))
                .add(16, 16, 16))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (receiptName.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please add a name to the receipt", "Warning", JOptionPane.WARNING_MESSAGE);
        } else if (ingredienceTable.getRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "Please add at least one ingredient before saving", "Warning", JOptionPane.WARNING_MESSAGE);
        } else {
            receipt.setName(receiptName.getText());

            if (createNew) {
                recipeDAO.create(receipt);
            } else {
                recipeDAO.update(receipt);
            }

            recipeListModel.setRecipes(recipeDAO.findAll());
            ingredientsTableModelRestaurant.setIngredients(receipt.getIngredients());

            dispose();
        }

    }//GEN-LAST:event_saveButtonActionPerformed

    private void addIngredientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addIngredientButtonActionPerformed
        new IngredientForReceiptDialog(ingredientsTableModel, ingredientDAO, recipeDAO, receipt).setVisible(true);
    }//GEN-LAST:event_addIngredientButtonActionPerformed

    private Ingredient getSelectedIngredient(int row) {
        String ingrName = String.valueOf(ingredienceTable.getValueAt(row, 0));
        for (Ingredient ingr : recipeIngredientList) {
            if (ingr.getName().equals(ingrName)) {
                return ingr;
            }
        }
        return null;
    }

    private void editIngredientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editIngredientActionPerformed
        if (ingredienceTable.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "Please select a ingredient you wish to edit.", "Warning", JOptionPane.WARNING_MESSAGE);
        } else {
            new IngredientDialog(ingredientsTableModel, ingredientDAO, getSelectedIngredient(ingredienceTable.getSelectedRow()), recipeDAO, receipt).setVisible(true);
        }
    }//GEN-LAST:event_editIngredientActionPerformed

    private void deleteIngredientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteIngredientActionPerformed
        if (ingredienceTable.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "Please select a ingredient you wish to delete.", "Warning", JOptionPane.WARNING_MESSAGE);
        } else {
            ingredient = getSelectedIngredient(ingredienceTable.getSelectedRow());

            recipeIngredientList.remove(ingredient);
            receipt.setIngredients(recipeIngredientList);

            ingredientsTableModel.setIngredients(recipeIngredientList);
        }
    }//GEN-LAST:event_deleteIngredientActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addIngredientButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton deleteIngredient;
    private javax.swing.JButton editIngredient;
    private javax.swing.JTable ingredienceTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel receiptLabel;
    private javax.swing.JTextField receiptName;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables
}
